﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using University.Workshop.Business;
using System.Configuration;
using System.Collections.ObjectModel;
using University.Workshop.Model;
using University.Workshop.DataAccess;

namespace University.Workshop.WebApp
{
    public partial class ViewProduct : System.Web.UI.Page
    {
        public static Collection<Product> products = new Collection<Product>();
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                products = ViewProductManager.GridLoad();
                ProductGrid.DataSource = products;
                ProductGrid.DataBind();
            }
        }

        protected void BtnSearch_Click(object sender, EventArgs e)
        {
            LabelMessage.Visible = false;
            try
            {
                products= SearchProductsManager.SearchedProducts(TxtBoxWords.Text, DropDownListCategory.SelectedItem.Text);
                ProductGrid.DataSource = products;
                ProductGrid.DataBind();
                LblResults.Visible = true;
                LblResults.Text =products.Count.ToString()+" Results";
            }
            catch (Exception ex)
            {
                LabelMessage.Visible = true;
                LabelMessage.Text = ex.Message;
                LblResults.Visible = false;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("AdvancedSearch.aspx");
        }

        protected void TxtBoxWords_TextChanged(object sender, EventArgs e)
        {
            
        }

        protected void OnPageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            ProductGrid.CurrentPageIndex = e.NewPageIndex;
            try
            {
                products = SearchProductsManager.SearchedProducts(TxtBoxWords.Text, DropDownListCategory.SelectedItem.Text);
                ProductGrid.DataSource = products;
                ProductGrid.DataBind();
            }
            catch (Exception er)
            {
                LabelMessage.Text = er.Message;
            }
        }

        protected void ProductGrid_EditCommand(object source, DataGridCommandEventArgs e)
        {
            int productSelected = e.Item.ItemIndex + (ProductGrid.CurrentPageIndex * ProductGrid.PageSize);
            LabelProdID.Text = products[productSelected].ProductId.ToString();
            LabelProdNAME.Text = products[productSelected].Name.ToString();
            LabelProdPrice.Text = products[productSelected].Price.ToString();
            string category = products[productSelected].Category.ToString();
            Product prod = new Product();
            prod = ProductDataAccess.GetProductById(Convert.ToInt64(LabelProdID.Text));
            if (prod.Status == "OnAuction")
            {
                Response.Redirect("Bid.aspx?id=" + LabelProdID.Text);
            }
            else
                Response.Redirect("ProductSelected.aspx?ProductID=" + LabelProdID.Text + "&ProductName=" + LabelProdNAME.Text + "&ProductPrice=" + LabelProdPrice.Text + "&ProductCategory=" + category);

        }
        
       

        

    }
}